<%--
  Created by IntelliJ IDEA.
  User: DongZhengWei
  Date: 2018/11/6
  Time: 9:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>

<div id="page-content">

	<div class="panel">
		<div class="panel-heading">
			<h3 class="panel-title">职权管理</h3>
		</div>
		<div class="panel-body">
			<div class="col-md-3">
				<ul id="tree_department" class="ztree"></ul>
			</div>
			<div class="col-md-9">
				<table id="power_table" cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered"></table>
			</div>
		</div>
	</div>

</div>

<script>

    $(function(){

        init_tree_department();

    });

    function init_tree_department() {

        var setting = {
            async: {
                enable: true,
                url:'<%=path%>/main/department/tree',
                autoParam: ['id'],
                type: 'GET',
                dataType: 'json',
                contentType : "application/json"
            },
            callback: {
                onAsyncSuccess: zTreeOnAsyncSuccess,
                onClick: zTreeOnClick
            },
            data: {
                simpleData: {
                    enable: true,
                    idKey: 'id',
                    pIdKey: 'pId',
                    rootPId: ''
                }
            },
            view: {
                autoCancelSelected: false,
                dblClickExpand: false,
                selectedMulti: false
            }
        };

        $.fn.zTree.init($('#tree_department'), setting);

    }

    //异步加载成功
	function zTreeOnAsyncSuccess(event, treeId, treeNode, msg){
        var zTree = $.fn.zTree.getZTreeObj('tree_department');
        if(treeNode){
            if(treeNode.id === '0'){
                var nodes = treeNode.children;
                zTree.selectNode(nodes[0]);
                init_dataTables_power(nodes[0].id);
			}
		}else{
            var rootNode = zTree.getNodes()[0];
            if (rootNode.isParent) {
                zTree.expandNode(rootNode, true, true, true);
            }
        }
	}

	//选中一个部门
    function zTreeOnClick(event, treeId, treeNode){
        var id = treeNode.id;
        if (id) {
            $("#power_table").DataTable().ajax.url("<%=path%>/main/power/list_department?departmentId=" + id).load();
        }
    }

    function init_dataTables_power(departmentId){
        var columns = [
	        {targets : 0, data : "id", title : "id", visible : false, orderable : false, searchable : false},
	        {targets : 1, data : "sortNo", title : "序号", visible : true, orderable : true, searchable : true},
	        {targets : 2, data : "name", title : "职权名称", visible : true, orderable : true, searchable : true},
	        {targets : 3, data : "parentName", title : "上级职权", visible : true, orderable : true, searchable : true},
	        {targets : 4, title : "操作", visible : true, orderable : false, searchable : false, render : actRender}
        ];

        var buttons = [
            {text : "添加职权", className : "btn btn-primary", enabled : true, action : add_power}
        ];

        __init_dataTables("power_table",'<%=path%>/main/power/list_department?departmentId='+departmentId,columns,buttons,false,true);
    }

    function actRender(data, type, row, meta){
        var id = row.id;
        data = '<button class="btn btn-default btn-xs" data-original-title="编辑" onclick="fn_power_edit(\'' + id + '\');"><i class="fa fa-pencil-square-o"> </i> 编辑</button> ';
        data += '<button class="btn btn-danger btn-xs" data-original-title="删除" onclick="fn_power_delete(\'' + id + '\');"><i class="fa fa-trash-o"> </i> 删除</button>';
        return data;
    }

    //职权编辑
    function fn_power_edit(id) {
        __open_dialog_form('编辑职权信息','<%=path%>/main/power/input?keyId=' + id, function (dialogRef) {
            var callback = function (data) {
                if (data) {
                    dialogRef.close();
                    $('#power_table').DataTable().ajax.reload(null, false);
                } else {
                    dialogRef.enableButtons();
                }
            };
            if (fn_power_input_save(callback) === false) {
                dialogRef.enableButtons();
            }
        });
    }

    //职权删除
	function fn_power_delete(id){
        var url = "<%=path%>/main/power/delete";
        __confirm_dialog(null,"确定要删除吗",function(){
            __ajax_post(url,{keyId : id},function(data){
                __toastr(data);
                $('#power_table').DataTable().ajax.reload(null, false);
			});
		});
	}

	//新建职权
    function add_power(){
        var zTree = $.fn.zTree.getZTreeObj('tree_department');
        var selectedNodes = zTree.getSelectedNodes();
        if (selectedNodes.length > 0 && selectedNodes[0].id!='0') {
            var departmentId = selectedNodes[0].id;
            __open_dialog_form('新增职权', '<%=path%>/main/power/input?departmentId='+departmentId, function (dialogRef) {
                var callback = function (data) {
                    if (data) {
                        dialogRef.close();
                        $('#power_table').DataTable().ajax.reload(null, false);
                    } else {
                        dialogRef.enableButtons();
                    }
                };
                if (fn_power_input_save(callback) === false) {
                    dialogRef.enableButtons();
                }
            });
        }else{
            __toastr_error("请先选择一个部门")
        }
    }

</script>